﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using ExamSharedClasses;

namespace ExamClient
{
    /// <summary>
    /// Diese Seite ist im Top-Bereich des Dockpanels des MainWindows. Hier gibt der
    /// der Benutzer seine E-Mail-Adresse und sein Kennwort ein und wird so am Server
    /// angemeldet. Fehlerausgaben erfolgen in der StatusBar des MainWindows.
    /// </summary>
    public partial class LoginBar : Page
    {    
        public LoginBar()
        {
            InitializeComponent();
            
            if (App.ServerConnection != null && App.ServerConnection.IsLoggedIn)
            {
                ChangeLoginToLogoutBar();
            }
            else
            {
                emailBox.Focus();
                loginBarGrid.Children.Remove(homeButton);
            }
            App.LoginBar = this;
            
        }

        /// <summary>
        /// An-/Abmeldeprozedur beim Klicken des Login/Logout-Buttons
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void loginButton_Click(object sender, RoutedEventArgs e)
        {
            if (loginButton.Content.Equals("Anmelden"))
            {
                string message = ClientMethods.IsUserInputValid(emailBox.Text, Tools.GenerateSHA1Hash(passwordBox.Password));
                if (message.Equals(""))
                {
                    try
                    {
                        App.UserAddress.UserEmail = emailBox.Text;
                        App.Password.PasswordHash = Tools.GenerateSHA1Hash(passwordBox.Password);
                        App.ServerConnection = new ServerConnection();
                        App.ServerConnection.Login();
                    }
                    catch (Exception ex)
                    {
                        App.Display("Anmeldung Exception:" +ex );
                    }

                }
                else
                {
                    App.MainWindow.SetStatusBarMessage(message);
                }
            }
            else
            {
                ClientMethods.ClientLogout();
            }
        }

        /// <summary>
        /// Methode zur Änderung der LoginBar zur LogoutBar
        /// </summary>
        public void ChangeLoginToLogoutBar()
        {
            emailLabel.Content = App.UserAddress.UserName + " " + App.UserAddress.UserSurName;
            emailBox.Visibility = Visibility.Hidden;
            passwordLabel.Visibility = Visibility.Hidden;
            passwordBox.Visibility = Visibility.Hidden;
            loginButton.Content = "Abmelden";
            loginButton.IsDefault = false;
            loginBarGrid.Children.Insert(0, homeButton);
        }

        /// <summary>
        /// Siehe ExamMode bei CLientMethods
        /// </summary>
        /// <param name="activate"></param>
        public void ExamMode(Boolean activate)
        {
            loginButton.IsEnabled = !activate;
            homeButton.IsEnabled = !activate;
        }

        /// <summary>
        /// Navigation zur Startseite
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void homeButton_Click(object sender, RoutedEventArgs e)
        {
            App.MainWindow.center.Content = App.SummaryPage;
        }  


       
    }
}
